package com.jabex.manager.actions;


import com.jabex.common.exceptions.ErrorFunctionalException;
import com.jabex.common.exceptions.ErrorOperationalException;
import com.jabex.common.exceptions.JabexException;
import com.jabex.common.exceptions.WarningFunctionalException;
import com.jabex.common.to.MessageWithParamsTO;
import com.jabex.common.to.ResponseMessageTO;
import com.jabex.manager.bundle.Messages;
import com.jabex.manager.util.MessageUtils;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.List;

/**
 * Created by jaugusto on 26/08/14.
 */
public abstract class Controller implements Serializable {

	private String messageBundle = Messages.BUNDLE_MESSAGE_NAME;

	public Controller() {
	}

	protected FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}

	protected void setMessages(ResponseMessageTO<?> response) {
		if (response.hasFunctionalErrors()) {
			for (ErrorFunctionalException e : response.getFunctionalErrors())
				setFunctionalErrorMessage(e);
		}
		if (response.hasErrorOperationalMessages()) {
			for (ErrorOperationalException e : response.getOperationalErrors())
				setErrorOperationalMessage(e);
		}
		if (response.hasWarns()) {
			for (WarningFunctionalException e : response.getWarns())
				setWarnMessage(e);
		}
		if (response.hasInfoMessages())
			setInfoMessage(response.getInfoMessages());
		if (response.hasFunctionalErrorMessages())
			setFunctionalErrorMessage(response.getErrorMessages());
		if (response.hasWarnMessages())
			setWarnMessage(response.getWarnMessages());

	}

	/**
	 * INFO MESSAGE
	 */

	protected void setInfoMessage(List<MessageWithParamsTO> messageIds) {
		if (messageIds != null) {
			for (MessageWithParamsTO m : messageIds)
				if (m.getMessage() != null)
					setInfoMessage(m.getMessage(), m.getParams());
		}
	}


	protected void setInfoMessage(String messageId) {
		setInfoMessage(messageId, (Object[]) null);
	}

	protected void setInfoMessage(JabexException e) {
		setInfoMessage(e.getMessage(), e.getParameters());
	}

	protected void setInfoMessage(UIComponent component, String messageId) {
		setInfoMessage(component, messageId, (Object[]) null);
	}

	protected void setInfoMessage(String messageId, Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_INFO, messageId, params);
	}

	protected void setInfoMessage(UIComponent component, String messageId,
	                              Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_INFO, messageId, params,
				component.getClientId(getFacesContext()));
	}

	/**
	 * WARNING*
	 */

	protected void setWarnMessage(String messageId) {
		setWarnMessage(messageId, (Object[]) null);
	}

	protected void setWarnMessage(List<MessageWithParamsTO> messageIds) {
		if (messageIds != null) {
			for (MessageWithParamsTO m : messageIds)
				if (m.getMessage() != null)
					setWarnMessage(m.getMessage(), m.getParams());
		}
	}

	protected void setWarnMessage(WarningFunctionalException e) {
		setWarnMessage(e.getMessage(), e.getParameters());
	}

	protected void setWarnMessage(UIComponent component, String messageId) {
		setWarnMessage(component, messageId, (Object[]) null);
	}

	protected void setWarnMessage(String messageId, Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_WARN, messageId, params);
	}

	protected void setWarnMessage(UIComponent component, String messageId,
	                              Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_WARN, messageId, params,
				component.getClientId(getFacesContext()));
	}

	/**
	 * ERRORES FUNCTIONAL
	 */
	protected void setFunctionalErrorMessage(String messageId) {
		setFunctionalErrorMessage(messageId, (Object[]) null);
	}

	protected void setFunctionalErrorMessage(List<MessageWithParamsTO> messageIds) {
		if (messageIds != null) {
			for (MessageWithParamsTO m : messageIds)
				if (m.getMessage() != null)
					setFunctionalErrorMessage(m.getMessage(), m.getParams());
		}
	}

	protected void setFunctionalErrorMessage(ErrorFunctionalException e) {
		setFunctionalErrorMessage(e.getMessage(), e.getParameters());
	}


	protected void setFunctionalErrorMessage(UIComponent component, String messageId) {
		setFunctionalErrorMessage(component, messageId, (Object[]) null);
	}

	protected void setFunctionalErrorMessage(String messageId, Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_ERROR, messageId, params);

	}

	protected void setFunctionalErrorMessage(UIComponent component, String messageId,
	                                         Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_ERROR, messageId, params,
				component.getClientId(getFacesContext()));
	}

	protected void setFunctionalErrorMessageWithClientId(String clientId, String messageId,
	                                                     Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_ERROR, messageId, params, clientId);
	}


	/**
	 * ERRORES OPERATIONAL
	 */

	protected void setErrorOperationalMessage(String messageId) {
		setErrorOperationalMessage(messageId, (Object[]) null);
	}

	protected void setErrorOperationalMessage(List<MessageWithParamsTO> messageIds) {
		if (messageIds != null) {
			for (MessageWithParamsTO m : messageIds)
				if (m.getMessage() != null)
					setErrorOperationalMessage(m.getMessage(), m.getParams());
		}
	}

	protected void setErrorOperationalMessage(ErrorOperationalException e) {
		setErrorOperationalMessage(e.getMessage(), e.getParameters());
	}


	protected void setErrorOperationalMessage(UIComponent component, String messageId) {
		setErrorOperationalMessage(component, messageId, (Object[]) null);
	}

	protected void setErrorOperationalMessage(String messageId, Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_FATAL, messageId, params);

	}

	protected void setErrorOperationalMessage(UIComponent component, String messageId,
	                                          Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_FATAL, messageId, params,
				component.getClientId(getFacesContext()));
	}

	protected void setErrorOperationalMessageWithClientId(String clientId, String messageId,
	                                                      Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_FATAL, messageId, params, clientId);
	}


	protected Object getSessionAttribute(String key) {
		return getFacesContext().getExternalContext().getSessionMap().get(key);
	}


}